load("//bazel:mongo_src_rules.bzl", "idl_generator", "mongo_cc_library", "mongo_cc_unit_test")

package(default_visibility = ["//visibility:public"])

exports_files(
    glob([
        "*.h",
        "*.cpp",
    ]),
)

idl_generator(
    name = "cluster_fsync_unlock_cmd_gen",
    src = "cluster_fsync_unlock_cmd.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
        "//src/mongo/idl:generic_argument_gen",
    ],
)

# These commands are registered with both the router and shard command
# registries.
mongo_cc_library(
    name = "sharded_cluster_sharding_commands",
    srcs = [
        "//src/mongo/db/router_role/routing_cache:flush_router_config_cmd.cpp",
        "//src/mongo/db/topology:get_shard_map_cmd.cpp",
    ],
    deps = [
        "//src/mongo/db:commands",
        "//src/mongo/db:index_key_validate",
        "//src/mongo/db:shard_role_api",
        "//src/mongo/db:sharding_expressions",
        "//src/mongo/db/fts:base_fts",
        "//src/mongo/db/index:expression_params",
        "//src/mongo/db/index:index_access_method",
        "//src/mongo/s:grid",
    ],
)

mongo_cc_library(
    name = "cluster_commands_common",
    srcs = [
        "document_shard_key_update_util.cpp",
        "strategy.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_explain.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_write_cmd.cpp",
    ],
    deps = [
        "//src/mongo/db:default_max_time_ms_cluster_parameter",
        "//src/mongo/db:fle_crud",
        "//src/mongo/db:internal_transactions_feature_flag",
        "//src/mongo/db:operation_time_tracker",
        "//src/mongo/db:read_write_concern_defaults",
        "//src/mongo/db:shared_request_handling",
        "//src/mongo/db:validate_api_parameters",
        "//src/mongo/db/commands:core",
        "//src/mongo/db/repl:repl_server_parameters",
        "//src/mongo/db/stats:api_version_metrics",
        "//src/mongo/db/stats:counters",
        "//src/mongo/db/transaction:transaction_api",
        "//src/mongo/executor:inline_executor",
        "//src/mongo/otel",
        "//src/mongo/otel/traces:tracing",
        "//src/mongo/rpc:metadata",
        "//src/mongo/rpc:rewrite_state_change_errors",
        "//src/mongo/s:load_balancer_support",
        "//src/mongo/s:mongos_topology_coordinator",
        "//src/mongo/s:sharding_api",
        "//src/mongo/s/query/planner:cluster_aggregate",
        "//src/mongo/transport:message_compressor",
        "//src/mongo/transport:service_executor",
        "//src/mongo/transport:transport_layer_common",
    ],
)

# These commands are registered only with the router-role.
mongo_cc_library(
    name = "cluster_commands",
    srcs = [
        "cluster_abort_transaction_cmd_s.cpp",
        "cluster_auto_split_vector_cmd.cpp",
        "cluster_build_info.cpp",
        "cluster_cleanup_reshard_collection_cmd.cpp",
        "cluster_commit_transaction_cmd_s.cpp",
        "cluster_compact_cmd.cpp",
        "cluster_coordinate_commit_txn.cpp",
        "cluster_filemd5_cmd.cpp",
        "cluster_fle2_cleanup_cmd.cpp",
        "cluster_fle2_compact_cmd.cpp",
        "cluster_fle2_get_count_info_cmd.cpp",
        "cluster_fsync_unlock_cmd.cpp",
        "cluster_ftdc_commands.cpp",
        "cluster_hello_cmd.cpp",
        "cluster_kill_op.cpp",
        "cluster_killoperations_cmd.cpp",
        "cluster_netstat_cmd.cpp",
        "cluster_oplog_note_cmd.cpp",
        "cluster_query_settings_cmds.cpp",
        "cluster_query_without_shard_key_cmd.cpp",
        "cluster_repl_set_get_status_cmd.cpp",
        "cluster_reshard_collection_cmd.cpp",
        "cluster_rwc_defaults_commands.cpp",
        "cluster_set_feature_compatibility_version_cmd.cpp",
        "cluster_shutdown_cmd.cpp",
        "cluster_transition_from_dedicated_config_server_cmd.cpp",
        "cluster_transition_to_dedicated_config_server_cmd.cpp",
        "cluster_validate_cmd.cpp",
        "cluster_whats_my_uri_cmd.cpp",
        "cluster_write_without_shard_key_cmd.cpp",
        "internal_transactions_test_command_s.cpp",
        "s_read_write_concern_defaults_server_status.cpp",
        ":cluster_fsync_unlock_cmd_gen",
        "//src/mongo/db/global_catalog/ddl:cluster_collmod_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_convert_to_capped_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_create_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_create_indexes_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_drop_collection_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_drop_database_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_drop_indexes_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_enable_sharding_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_list_collections_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_list_databases_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_list_indexes_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_refine_collection_shard_key_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_rename_collection_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_repair_sharded_collection_chunks_history_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_reset_placement_history_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_set_allow_migrations_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_shard_collection_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_untrack_unsplittable_collection.cpp",
        "//src/mongo/db/global_catalog/ddl:refine_collection_shard_key_gen",
        "//src/mongo/db/global_catalog/ddl:shard_collection_gen",
        "//src/mongo/db/shard_role/lock_manager:cluster_lock_info_cmd.cpp",
        "//src/mongo/db/shard_role/shard_catalog:cluster_coll_stats_cmd.cpp",
        "//src/mongo/db/shard_role/shard_catalog:cluster_db_stats_cmd.cpp",
        "//src/mongo/db/shard_role/shard_catalog:cluster_validate_db_metadata_cmd.cpp",
        "//src/mongo/db/sharding_environment:cluster_commands_gen",
        "//src/mongo/db/sharding_environment:cluster_is_db_grid_cmd.cpp",
        "//src/mongo/db/sharding_environment:cluster_multicast_cmd.cpp",
        "//src/mongo/db/topology/cluster_parameters:cluster_get_cluster_parameter_cmd.cpp",
        "//src/mongo/db/topology/cluster_parameters:cluster_set_cluster_parameter_cmd.cpp",
        "//src/mongo/db/topology/user_write_block:cluster_set_user_write_block_mode_command.cpp",
        "//src/mongo/db/versioning_protocol:cluster_get_database_version_cmd.cpp",
        "//src/mongo/db/versioning_protocol:cluster_get_shard_version_cmd.cpp",
        "//src/mongo/s/commands/commit_quorum:cluster_set_index_commit_quorum_cmd.cpp",
        "//src/mongo/s/commands/data_movement:cluster_abort_move_collection_cmd.cpp",
        "//src/mongo/s/commands/data_movement:cluster_abort_reshard_collection_cmd.cpp",
        "//src/mongo/s/commands/data_movement:cluster_abort_rewrite_collection_cmd.cpp",
        "//src/mongo/s/commands/data_movement:cluster_abort_unshard_collection_cmd.cpp",
        "//src/mongo/s/commands/data_movement:cluster_commit_reshard_collection_cmd.cpp",
        "//src/mongo/s/commands/data_movement:cluster_move_collection_cmd.cpp",
        "//src/mongo/s/commands/data_movement:cluster_rewrite_collection_cmd.cpp",
        "//src/mongo/s/commands/data_movement:cluster_unshard_collection_cmd.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_analyze_cmd.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_bulk_write_cmd_s.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_count_cmd_s.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_current_op.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_distinct_cmd.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_explain_cmd.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_find_and_modify_cmd.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_find_cmd_s.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_getmore_cmd_s.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_index_filter_cmd.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_killcursors_cmd.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_map_reduce_agg.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_map_reduce_cmd.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_pipeline_cmd_s.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_plan_cache_clear_cmd.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_profile_cmd.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_release_memory_cmd.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_write_cmd_s.cpp",
        "//src/mongo/s/commands/storage_engine:cluster_data_size_cmd.cpp",
        "//src/mongo/s/commands/storage_engine:cluster_fsync_cmd.cpp",
    ],
    deps = [
        ":cluster_commands_common",
        "//src/mongo/db:api_parameters",
        "//src/mongo/db:change_stream_options_manager",
        "//src/mongo/db:dbcommands_idl",
        "//src/mongo/db:fle_crud",
        "//src/mongo/db:internal_transactions_feature_flag",
        "//src/mongo/db:read_write_concern_defaults",
        "//src/mongo/db:server_base",
        "//src/mongo/db:server_feature_flags",
        "//src/mongo/db:shared_request_handling",
        "//src/mongo/db:validate_api_parameters",
        "//src/mongo/db/auth:auth_checks",
        "//src/mongo/db/commands:buildinfo_common",
        "//src/mongo/db/commands:cluster_server_parameter_commands_invocation",
        "//src/mongo/db/commands:core",
        "//src/mongo/db/commands:create_command",
        "//src/mongo/db/commands:kill_common",
        "//src/mongo/db/commands:list_databases_command",
        "//src/mongo/db/commands:profile_common",
        "//src/mongo/db/commands:rwc_defaults_commands",
        "//src/mongo/db/commands:servers",
        "//src/mongo/db/commands:set_feature_compatibility_version_idl",
        "//src/mongo/db/commands:set_index_commit_quorum_idl",
        "//src/mongo/db/commands:shutdown_idl",
        "//src/mongo/db/commands:test_commands_enabled",
        "//src/mongo/db/commands/query_cmd:bulk_write_common",
        "//src/mongo/db/commands/query_cmd:bulk_write_parser",
        "//src/mongo/db/commands/query_cmd:current_op_common",
        "//src/mongo/db/commands/query_cmd:map_reduce_parser",
        "//src/mongo/db/commands/query_cmd:search_index_commands",
        "//src/mongo/db/ftdc:ftdc_server",
        "//src/mongo/db/matcher:expressions_mongod_only",
        "//src/mongo/db/query:command_request_response",
        "//src/mongo/db/query:map_reduce_output_format",
        "//src/mongo/db/query/client_cursor:cursor_response_idl",
        "//src/mongo/db/query/query_settings:query_settings_service",
        "//src/mongo/db/query/query_shape",
        "//src/mongo/db/query/search:search_index_process_router",
        "//src/mongo/db/query/util:cluster_find_util",
        "//src/mongo/db/query/write_ops:parsed_update",
        "//src/mongo/db/repl:repl_server_parameters",
        "//src/mongo/db/repl/hello:hello_auth",
        "//src/mongo/db/repl/hello:hello_command",
        "//src/mongo/db/shard_role",
        "//src/mongo/db/shard_role/ddl:coll_mod_command_idl",
        "//src/mongo/db/shard_role/ddl:index_commands_idl",
        "//src/mongo/db/shard_role/ddl:rename_collection_idl",
        "//src/mongo/db/shard_role/lock_manager:lock_info_command",
        "//src/mongo/db/shard_role/shard_catalog:collection_uuid_mismatch_info",
        "//src/mongo/db/shard_role/shard_catalog:validate_db_metadata_command",
        "//src/mongo/db/stats:api_version_metrics",
        "//src/mongo/db/stats:counters",
        "//src/mongo/db/timeseries:timeseries_conversion_util",
        "//src/mongo/db/timeseries:timeseries_options",
        "//src/mongo/db/topology/cluster_parameters:cluster_server_parameter_cmds_idl",
        "//src/mongo/db/topology/cluster_parameters:cluster_server_parameter_refresher",
        "//src/mongo/db/topology/user_write_block:set_user_write_block_mode_idl",
        "//src/mongo/db/transaction:transaction_api",
        "//src/mongo/db/views",
        "//src/mongo/executor:async_multicaster",
        "//src/mongo/executor:async_request_executor",
        "//src/mongo/executor:inline_executor",
        "//src/mongo/rpc:rewrite_state_change_errors",
        "//src/mongo/s:grid",
        "//src/mongo/s:load_balancer_support",
        "//src/mongo/s:mongos_topology_coordinator",
        "//src/mongo/s:sharding_api",
        "//src/mongo/s/commands/query_cmd:cluster_replicate_search_index_command_idl",
        "//src/mongo/s/commands/query_cmd:populate_cursor",
        "//src/mongo/s/query/exec:cluster_cursor",
        "//src/mongo/s/query/planner:cluster_aggregate",
        "//src/mongo/transport:message_compressor",
        "//src/mongo/transport:transport_layer_common",
    ] + select({
        "//bazel/config:enterprise_feature_audit_enabled": [
            "//src/mongo/db/modules/enterprise/src/audit:audit_mongos",
        ],
        "//conditions:default": [],
    }),
)

# These commands are registered with the router role only.
mongo_cc_library(
    name = "sharded_cluster_commands",
    srcs = [
        "cluster_analyze_shard_key_cmd.cpp",
        "cluster_balancer_collection_status_cmd.cpp",
        "cluster_clear_jumbo_flag_cmd.cpp",
        "cluster_configure_collection_balancing.cpp",
        "cluster_configure_query_analyzer_cmd.cpp",
        "cluster_control_balancer_cmd.cpp",
        "cluster_move_chunk_cmd.cpp",
        "cluster_move_range_cmd.cpp",
        "cluster_split_cmd.cpp",
        "cluster_split_vector_cmd.cpp",
        "cluster_update_zone_key_range_cmd.cpp",
        "cluster_user_management_commands.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_add_shard_to_zone_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_change_primary_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_create_unsplittable_collections_command.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_list_shards_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_merge_all_chunks_on_shard_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_merge_chunks_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_move_primary_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:cluster_remove_shard_from_zone_cmd.cpp",
        "//src/mongo/db/global_catalog/ddl:list_shards_gen",
        "//src/mongo/db/global_catalog/metadata_consistency_validation:cluster_check_metadata_consistency_cmd.cpp",
        "//src/mongo/db/topology:cluster_add_shard_cmd.cpp",
        "//src/mongo/db/topology:cluster_commit_shard_removal_cmd.cpp",
        "//src/mongo/db/topology:cluster_commit_transition_to_dedicated_config_server_cmd.cpp",
        "//src/mongo/db/topology:cluster_get_transition_to_dedicated_config_server_status_cmd.cpp",
        "//src/mongo/db/topology:cluster_remove_shard_cmd.cpp",
        "//src/mongo/db/topology:cluster_shard_draining_status_cmd.cpp",
        "//src/mongo/db/topology:cluster_start_shard_draining_cmd.cpp",
        "//src/mongo/db/topology:cluster_start_transition_to_dedicated_config_server_cmd.cpp",
        "//src/mongo/db/topology:cluster_stop_shard_draining_cmd.cpp",
        "//src/mongo/db/topology:cluster_stop_transition_to_dedicated_config_server_cmd.cpp",
    ],
    deps = [
        ":cluster_commands",
        ":cluster_commands_common",
        "//src/mongo/db:audit",
        "//src/mongo/db:commands",
        "//src/mongo/db:server_base",
        "//src/mongo/db/auth",
        "//src/mongo/db/auth:address_restriction",
        "//src/mongo/db/auth:auth_checks",
        "//src/mongo/db/auth:builtin_roles",
        "//src/mongo/db/auth:saslauth",
        "//src/mongo/db/commands:servers",
        "//src/mongo/db/commands:umc_commands_idl",
        "//src/mongo/db/global_catalog/metadata_consistency_validation:metadata_consistency_types_idl",
        "//src/mongo/rpc:client_metadata",
        "//src/mongo/rpc:rewrite_state_change_errors",
        "//src/mongo/s:analyze_shard_key_common",
        "//src/mongo/s:grid",
        "//src/mongo/s:mongos_topology_coordinator",
        "//src/mongo/s:sharding_api",
        "//src/mongo/transport:transport_layer_common",
    ],
)

mongo_cc_unit_test(
    name = "s_commands_test",
    srcs = [
        "document_shard_key_update_test.cpp",
        "//src/mongo/db/shard_role/shard_catalog:cluster_validate_db_metadata_cmd_test.cpp",
        "//src/mongo/db/sharding_environment:cluster_command_test_fixture.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_aggregate_test.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_bulk_write_test.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_bulk_write_uwe_test.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_delete_test.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_distinct_test.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_explain_test.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_find_and_modify_test.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_find_test.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_insert_test.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_profile_cmd_test.cpp",
        "//src/mongo/s/commands/query_cmd:cluster_update_test.cpp",
    ],
    tags = ["mongo_unittest_fifth_group"],
    deps = [
        ":cluster_commands",
        ":cluster_commands_common",
        "//src/mongo/db:read_write_concern_defaults_mock",
        "//src/mongo/db:vector_clock",
        "//src/mongo/db/commands:profile_cmd_test_utils",
        "//src/mongo/db/pipeline/process_interface:mongos_process_interface_factory",
        "//src/mongo/db/shard_role:service_context_non_d",
        "//src/mongo/db/sharding_environment:sharding_mongos_test_fixture",
        "//src/mongo/s/query/planner:cluster_aggregate",
        "//src/mongo/transport:service_executor",
    ],
)
